function format(data){
    let str="";
    for(let key in data){
        str+=`&${key}=${data[key]}`;
    }
    str=str.slice(1);
    return str;
}
const BASE_URL="";
const axios={
    get:(url,data)=>{
        return new Promise((resolve,reject)=>{
            let xhr=new XMLHttpRequest();
            xhr.open('get',BASE_URL+url+"?"+format(data));
            xhr.send();
            xhr.onreadystatechange=()=>{
                if(xhr.readyState===4 && xhr.status===200){
                    let res=JSON.parse(xhr.responseText);
                    resolve(res);
                }
            }
        })
    },
    post:(url,data)=>{
        return new Promise(resolve=>{
            let xhr=new XMLHttpRequest();
            xhr.open('post',BASE_URL+url);
            xhr.setRequestHeader('content-type','application/json');
            xhr.send(JSON.stringify(data));
            xhr.onreadystatechange=()=>{
                if(xhr.readyState===4 && xhr.status===200){
                    let res=JSON.parse(xhr.responseText);
                    resolve(res);
                }
            }
        })
    },
    ajax:obj=>{
        if(obj.type==="get"){
            this.get(obj.url,obj.data);
        }
        else if(obj.type==="post"){
            this.post(obj.url,obj.data);
        }
    }
}
window.axios=axios;